package isquirrel;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import isquirrel.beans.User;
import isquirrel.error.UsernameAlreadyUsedException;
import isquirrel.util.HibernateUtil;

import java.util.Date;

import org.junit.Test;

public class UserManagerTest {
	
	private void createTestUser1() throws UsernameAlreadyUsedException  {
		UserManager.registerUser("Avgoustinos","Kadis", "phidias","test", new Date(), "Male", null,0);
	}
	
	private void createTestUser2() throws UsernameAlreadyUsedException  {
		UserManager.registerUser("User2","U2","phidias","test",new Date(),"Male",null,0);
	}
	
	private void createTestUser3() throws UsernameAlreadyUsedException  {
		UserManager.registerUser("User3","U3","user3","test",new Date(),"Female",null,0);
	}

	@Test
	public void testRegisterUser() {
		HibernateUtil.useTestDB();
		HibernateUtil.beginTransaction();
		
		try {
			createTestUser1();
		} catch (UsernameAlreadyUsedException e) {
			fail("Couln't register user");
		}
		
		boolean throwedException = false;
		try {
			createTestUser2(); //user with the same username
		} catch (UsernameAlreadyUsedException e) {
			throwedException = true;
		}
		assertTrue(throwedException);
		
		HibernateUtil.commit();
	}

	@Test
	public void testGetUser() {
		HibernateUtil.useTestDB();
		HibernateUtil.beginTransaction();

		try {
			createTestUser3();
		} catch (UsernameAlreadyUsedException e) {
			fail("shouldn't come here");
		}
		
		User u3 = UserManager.getUser("user3");
		
		assertTrue(u3.getFirstName() == "User3");
		
		HibernateUtil.commit();
		
	}

}
